<template>
  <div class="article-list">
    <van-pull-refresh
    v-model="isreFreshLoading"
    :success-text="refreshSuccessText"
    :success-duration="1500"
    @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad">
        <article-item v-for="(article, index) in list" :key="index" :article="article"/>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
import ArticleItem from '@/components/article-item'
export default {
  name: 'ArticleList',
  components: {
    ArticleItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      isreFreshLoading: false,
      refreshSuccessText: '',
      error: false,
      timestamp: null// 请求获取下一页的时间戳
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {
  },
  methods: {
    // 初始化或者滚动会触发的事件
    // onLoad () {
    //   setTimeout(() => {
    //     for (let i = 0; i < 10; i++) {
    //       this.list.push(this.list.length + 1)
    //     }
    //     //  加载状态结束
    //     this.loading = false
    //     if (this.list.length >= 30) {
    //       this.finished = true
    //     }
    //   }, 1000)
    // }
    async onLoad () {
      try {
        // 请求数据
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(), // 时间戳
          with_top: 1
        })
        // console.log(data)
        // 把数据添加到list中
        const { results } = data.data
        // console.log(results)
        // ...数组的展开操作符会把数组中的元素一个个的拿出来
        this.list.push(...results)
        this.loading = false
        // 判断数据是否加载完成
        if (results.length) {
          // 更新获取下一页的时间戳
          this.timestamp = data.data.pre_timestamp
        } else {
          this.finished = true
          // 加载完成则停止加载
        }
      } catch (err) {
        console.log(err)
        this.$toast('请求数据失败')
        this.loading = false // 关闭 loading 效果
        this.error = true // 开启错误提示
      }
    },
    async onRefresh () {
      try {
        // 获取数据
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: Date.now(), // 时间戳
          with_top: 1
        })
        // 将数据添加到顶部
        const { results } = data.data
        this.list.unshift(...results)
        // 关闭刷新的状态
        this.isreFreshLoading = false
        // 提示成功
        this.refreshSuccessText = `刷新成功，更新了${results.length}条数据`
      } catch (err) {
        console.log('获取信息失败', err)
        this.refreshSuccessText = '刷新失败'
        this.isreFreshLoading = false
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-list {
  // 百分比单位是相对于父元素的
  height: 100%;
  // 1vh = 视口高度的百分之一
  // 1vw = 视口宽度的百分之一
  height: 79vh;
  overflow-y: auto;
}
</style>
